package com.test.qnews.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.test.qnews.R;
import com.test.qnews.adapter.QuestionAdapter;
import com.test.qnews.beans.GsonQuestionBeans;
import com.test.qnews.beans.QuestionBeans;
import com.test.qnews.volley.VolleySingleton;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/20.
 */
public class QuestionActivity extends BaseActivity{
    private String url = "http://c.3g.163.com/newstopic/list/expert/0-10.html";
    private List<QuestionBeans> datas;
    private QuestionAdapter topicAdapter;
    private ListView listView;


    @Override
    protected int setLayout() {
        return R.layout.activity_question;
    }

    @Override
    protected void initView() {
        listView = (ListView) findViewById(R.id.lv_question);
        data();
    }

    private void data() {

        VolleySingleton.getInstance();
        VolleySingleton.getInstance().addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                datas = new ArrayList<>();
                GsonQuestionBeans gsonBean = gson.fromJson(response, GsonQuestionBeans.class);

                for (int i = 0; i < gsonBean.getData().getExpertList().size(); i++) {
                    QuestionBeans questionBeans = new QuestionBeans();

                    questionBeans.setPicurl(gsonBean.getData().getExpertList().get(i).getPicurl());
                    questionBeans.setHeadpicurl(gsonBean.getData().getExpertList().get(i).getHeadpicurl());
                    questionBeans.setName(gsonBean.getData().getExpertList().get(i).getName());
                    questionBeans.setAlias(gsonBean.getData().getExpertList().get(i).getAlias());
                    questionBeans.setDescription(gsonBean.getData().getExpertList().get(i).getDescription());
                    questionBeans.setTitle(gsonBean.getData().getExpertList().get(i).getTitle());
                    questionBeans.setExpertId(gsonBean.getData().getExpertList().get(i).getExpertId());
                    datas.add(questionBeans);
                    topicAdapter = new QuestionAdapter(QuestionActivity.this,datas);
                    listView.setAdapter(topicAdapter);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(QuestionActivity.this, DetailsQuestionActivity.class);
                            intent.putExtra("pos", position);
                            intent.putExtra("data", (Serializable) datas);
                            startActivity(intent);
                        }
                    });



                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

    }
}
